package com.kaifa.test;
import java.util.Random;
public class Demo6{
	public static void main(String[] args) {
		//有序排列数组（大到小，小到大无所谓）
		Random rm = new Random();
		int[] array = new int[1000];
		for (int i = 0 ; i < array.length ; i++) {
			array[i] = rm.nextInt(10000);
		}

//		long start = System.currentTimeMillis();
//		int[] intArray = searchRecursive(array);
//		String str = "";
//		for(int s :intArray){
//			str+=","+s;
//		}
//		long end = System.currentTimeMillis();
//		System.out.println("二分法排序耗时：" + (end -start) + "; 排序后的数组是："+str);

		long start1 = System.currentTimeMillis();
		int[] sortedArr1 = bubbleSort(array);
		long end1 = System.currentTimeMillis();
		String str2 = "";
		for(int str3:sortedArr1){
			str2 +=","+str3;
		}
		System.out.println("冒泡排序耗时：" + (end1 -start1) + "; 排序后的数组是："+str2);
	}

	public static int[] searchRecursive(int[] array){
		int[] temp = new int[array.length];
		for(int i = 0;i<temp.length;i++){
			if(i==0){
				temp[i]=array[i];
			}else{
				int j = 0;
				for(int k=i-1;j<i&&k>=0;){
					if(temp[(j+k)/2]>=array[i]){
						if((j+k)/2==0){
							for(int n=i;n>0;n--){
								temp[n]=temp[n-1];
							}
							temp[0]=array[i];
							break;
						}else if(temp[(j+k)/2]<=array[i]){
							for(int n=i;n>(j+k)/2;n--){
								temp[n]=temp[n-1];
							}
							temp[(j+k)/2]=array[i];
							break;
						}else{
							k = (k+j)/2-1;
						}
					}else if(temp[(j+k)/2]<=array[i]){
						if((k+j)/2==i-1){
							temp[i]=array[i];
							break;
						}else{
							j=(k+j)/2+1;
						}
					}
				}
			}
		}
		return temp;
	} 
	

	public static int[] bubbleSort(int[] array){
		int num = 0;
		for(int i=array.length-1;i<=0;i--){
			for(int j=0;j<i;j++){
				if(array[j]>array[j+1]){
					num = array[i];
					array[i] = array[i+1];
					array[i+1] = num;
				}
			}
		}
		return array;
	}
}